package com.sentiment.algorithm;

public class Matrix {
    private int rows;       //行数
    private int cols;        //列数
    private int a[][];

    public Matrix(int rows, int cols) {
        this.rows = rows;
        this.cols = cols;
        a=new int[rows][cols];
    }

    @Override
    public String toString() {
        StringBuilder str=new StringBuilder();
        for(int i=0;i<cols;i++){
            for(int j=0;j<rows;j++){
                str.append(a[j][i]);
                str.append(",");
            }
            str.append("\n");
        }
        return str.toString();
    }


    public String tosimilarpairCore(){
        StringBuilder stringBuilder=new StringBuilder();
        for(int i=0;i<rows;i++){
            for (int j=0;j<cols;j++){
                if(a[i][j]!=0){
                    stringBuilder.append(i+"::"+j+">>"+a[i][j]+"%");
                    stringBuilder.append("\n");

                }
            }
        }
        return stringBuilder.toString();
    }

    public String tosimilarpair(){
        StringBuilder stringBuilder=new StringBuilder();
        for(int i=0;i<rows;i++){
            for (int j=0;j<cols;j++){
                if(a[i][j]!=0){
                    stringBuilder.append(i+"::"+j+">>"+a[i][j]+"%");
                    stringBuilder.append("\n");
                    a[j][i]=0;
                }
            }
        }
        return stringBuilder.toString();
    }
    /*
     *计算i和j的签名相似度
     */
    public double calSimilar(int col1,int col2){
        int count=0;
        for(int k=0;k<rows;k++){
            if(a[k][col1]==a[k][col2]){
                count=count+1;
            }
        }
        double t1=(double)count;
        double t2=(double)rows;
        return t1/t2;
    }

    public double calSimilarCore(int row1,int row2){
        int count=0;
        for(int k=0;k<cols;k++){
            if(a[row1][k]==a[row2][k]){
                count=count+1;
            }
        }
        double t1=(double)count;
        double t2=(double)cols;
        return t1/t2;
    }

    public void setNumberWithRowAndCol(int row,int col,int number){
        if(row<rows&&row>=0&&col<cols&&col>=0){
            a[row][col]=number;
            return;
        }
        throw new IndexOutOfBoundsException("out of index!Error !!");
    }
    public int getNumberWithLineAndRow(int row,int col){
        if(row<rows&&row>=0&&col<cols&&col>=0){
            return a[row][col];
        }
        throw new IndexOutOfBoundsException("out of index!Error !!");
    }
    public String getKeyStringWithLines(int length,int row,int col){
        if(row+length>rows){
            throw new IndexOutOfBoundsException("error in lines");
        }
        String str="";
        for(int i=0;i<length;i++){
            str+=a[row+i][col];
        }
        return str;
    }

    public String getKeyStringWithRows(int length,int row,int col){
        if(col+length>cols){
            throw new IndexOutOfBoundsException("error with rows!!");
        }
        String str="";
        for(int i=0;i<length;i++){
            str+=a[row][col+i];
        }
        return str;
    }


    public int getRows() {
        return rows;
    }

    public int getCols() {
        return cols;
    }
}
